package com.hancai.barrage.web.controller;

import com.hancai.barrage.model.JsonResult;
import com.hancai.barrage.model.Page;
import com.hancai.barrage.model.TreeData;
import com.hancai.barrage.pagehelper.QueryObject;
import com.hancai.barrage.pojo.Department;
import com.hancai.barrage.service.SysDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 涵菜
 * @version 1.0
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private SysDepartmentService departmentService;
    @RequestMapping("/listAll")
    public JsonResult<List<Department>> listAll(){
        return JsonResult.success(departmentService.selectAll());
    }
    @RequestMapping("/list")
    public JsonResult<Page<Department>> query(QueryObject qo) {
        return JsonResult.success(departmentService.query(qo));
    }
    @RequestMapping("/treeList")
    public JsonResult<List<TreeData>>treeList(){
        //[{id:1,label:'',children:[{id:3,label:''},{id:4,label:'',children:[]}]}}]
        List<TreeData>treeList=departmentService.queryTreeList();
        return JsonResult.success(treeList);
    }
    @RequestMapping("/saveOrUpdate")
    //requestBody：当前请求的请求体就会为当前注解所标识的形参赋值
    public JsonResult<?>saveOrUpdate(@RequestBody Department department){
        if(department.getId()!=null){
            departmentService.updateById(department);
        }else {
            departmentService.save(department);
        }
        return JsonResult.success();
    }
    @RequestMapping("/delete")
    public JsonResult<?>delete(Long id){
        if(id==null) {
            return JsonResult.failed("你的id不能为空");
        }
        departmentService.deleteById(id);
        return JsonResult.success();
    }
}
